package cn.hzking.gjmall.adpters.order;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.text.Format;
import java.util.ArrayList;
import java.util.List;

import cn.hzking.gjmall.R;
import cn.hzking.gjmall.activitys.order.OrderDetailActivity;
import cn.hzking.gjmall.activitys.wuliu.WuliuDetailActivity;
import cn.hzking.gjmall.bean.OrderBean;
import cn.hzking.gjmall.bean.OrderSecondBean;
import cn.hzking.gjmall.utils.FormatUtil;

/**
 * Created by Administrator on 2018/3/2.
 */

public class OrderAdapter extends BaseQuickAdapter<OrderBean.DataBean.OrderListBean.ModuleBean,BaseViewHolder> {
    private Operation operation;
    public OrderAdapter(int layoutResId, @Nullable List<OrderBean.DataBean.OrderListBean.ModuleBean> data) {
        super(layoutResId, data);
    }


    public Operation getOperation() {
        return operation;
    }

    public void setOperation(Operation operation) {
        this.operation = operation;
    }

    @Override
    protected void convert(BaseViewHolder helper, final OrderBean.DataBean.OrderListBean.ModuleBean item) {
        final int orderId=item.getId();
        final int orderGroupId=item.getOrdergroupId();
        RecyclerView recyclerView = helper.getView(R.id.order_rv);
        TextView shopnameTv=helper.getView(R.id.tv_shopname);
        TextView ordernumTv=helper.getView(R.id.tv_ordernum);
        TextView orderstatusTv=helper.getView(R.id.tv_orderstatus);
        TextView orderamountTv=helper.getView(R.id.tv_orderamount);
        TextView payTv=helper.getView(R.id.order_btn_pay);
        TextView cancelTv=helper.getView(R.id.order_btn_cancel);
        TextView wuliuTv=helper.getView(R.id.order_btn_wuliu);
        TextView sureTv=helper.getView(R.id.order_btn_sure);
        TextView deleteTv=helper.getView(R.id.order_btn_delete);
        RelativeLayout btnRl=helper.getView(R.id.rl_btn);
        shopnameTv.setText(item.getMemberName());
        ordernumTv.setText("订单编号: "+item.getOrderno());
        orderstatusTv.setText(item.getOrderStatusStr());
        orderamountTv.setText("订单金额: ¥"+ FormatUtil.format(item.getOrderPrice()));
        switch (item.getStatus()){
            case "WAIT_BUYER_PAY"://待支付
                btnRl.setVisibility(View.VISIBLE);
                payTv.setVisibility(View.VISIBLE);
                cancelTv.setVisibility(View.VISIBLE);
                wuliuTv.setVisibility(View.GONE);
                sureTv.setVisibility(View.GONE);
                deleteTv.setVisibility(View.GONE);
                break;
            case "WAIT_SEND_GOODS"://待发货
                if(item.isWhetherVirtual()){
                    btnRl.setVisibility(View.GONE);
                }else {
                    btnRl.setVisibility(View.VISIBLE);
                    payTv.setVisibility(View.GONE);
                    cancelTv.setVisibility(View.GONE);
                    wuliuTv.setVisibility(View.VISIBLE);
                    sureTv.setVisibility(View.GONE);
                    deleteTv.setVisibility(View.GONE);
                }
                break;
            case "ALREADY_SEND"://待收货
                btnRl.setVisibility(View.VISIBLE);
                payTv.setVisibility(View.GONE);
                cancelTv.setVisibility(View.GONE);
                sureTv.setVisibility(View.VISIBLE);
                deleteTv.setVisibility(View.GONE);
                if(item.isWhetherVirtual()){
                    wuliuTv.setVisibility(View.GONE);
                }else {
                    wuliuTv.setVisibility(View.VISIBLE);
                }

                break;
            case "TRADE_CLOSED"://交易关闭
                btnRl.setVisibility(View.VISIBLE);
                payTv.setVisibility(View.GONE);
                cancelTv.setVisibility(View.GONE);
                wuliuTv.setVisibility(View.GONE);
                sureTv.setVisibility(View.GONE);
                deleteTv.setVisibility(View.VISIBLE);
                break;
            case "REFUNDED_CLOSED"://退款
                btnRl.setVisibility(View.VISIBLE);
                payTv.setVisibility(View.GONE);
                cancelTv.setVisibility(View.GONE);
                wuliuTv.setVisibility(View.GONE);
                sureTv.setVisibility(View.GONE);
                deleteTv.setVisibility(View.VISIBLE);
                break;
            case "TRADE_FINISHED"://交易完成
                btnRl.setVisibility(View.VISIBLE);
                payTv.setVisibility(View.GONE);
                cancelTv.setVisibility(View.GONE);
                if(item.isWhetherVirtual()){
                    wuliuTv.setVisibility(View.GONE);
                }else {
                    wuliuTv.setVisibility(View.VISIBLE);
                }
                sureTv.setVisibility(View.GONE);
                deleteTv.setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }
        sureTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                operation.sure(orderId);
            }
        });
        deleteTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                operation.delete(orderId);
            }
        });


        helper.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mContext, OrderDetailActivity.class);
                intent.putExtra("orderId",orderId);
                intent.putExtra("orderGroupId",orderGroupId);
                mContext.startActivity(intent);
            }
        });
        wuliuTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mContext, WuliuDetailActivity.class);
                intent.putExtra("orderId",orderId);
                mContext.startActivity(intent);
            }
        });
        cancelTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mContext, OrderDetailActivity.class);
                intent.putExtra("orderId",orderId);
                intent.putExtra("orderGroupId",orderGroupId);
                mContext.startActivity(intent);
            }
        });
        payTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mContext, OrderDetailActivity.class);
                intent.putExtra("orderId",orderId);
                intent.putExtra("orderGroupId",orderGroupId);
                mContext.startActivity(intent);
            }
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(linearLayoutManager);
        OrderSecondAdapter orderSecondAdapter = new OrderSecondAdapter(R.layout.item_order_gooddetail, null);
        orderSecondAdapter.setOrderId(orderId);
        recyclerView.setAdapter(orderSecondAdapter);
        orderSecondAdapter.setNewData(item.getOrderItems());
    }


    public interface Operation{
        public abstract void delete(int orderId);
        public abstract void sure(int orderId);
    }
}
